package com.powernode.config;

import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

import javax.annotation.Resource;
import java.util.HashSet;

/**
 * @Author 梁卓
 * @Version 1.0
 * @ClassName SwaggerAutoConfig
 * @Description Swagger自动装配
 * @Date 2024/07/21 14:30
 */
@Configuration
@EnableConfigurationProperties(SwaggerProperties.class)
public class SwaggerAutoConfiguration {

    @Resource
    private SwaggerProperties swaggerProperties;

    /**
     * 项目环境对象
     */
    @Resource
    private Environment environment;

    /**
     * 设置swagger文档对象
     * 1.swagger文档只有在开发阶段生成，生产环境不需要生成文档
     * @return 结果
     */
    @Bean
    public Docket docket() {
        // 生成文档的开关，true默认开启状态
        Boolean flag = true;
        // 从项目环境对象中获取项目运行环境标识
        String[] activeProfiles = environment.getActiveProfiles();
        // 循环遍历
        for (String activeProfile : activeProfiles) {
            if ("pro".equals(activeProfile)) {
                // pro一般表示生产环境
                flag = false;
                break;
            }
        }

        return new Docket(DocumentationType.OAS_30)
                // 文档描述信息对象
                .apiInfo(getApiInfo())
                // 是否生成文档的开关
                .enable(flag)
                .select()
                // 基于包名描述swagger注解
                .apis(RequestHandlerSelectors.basePackage(swaggerProperties.getBasePackage()))
                .build();
    }

    private ApiInfo getApiInfo() {
        Contact contact = new Contact(
                swaggerProperties.getName(),
                swaggerProperties.getUrl(),
                swaggerProperties.getEmail()
        );

        return new ApiInfo(
                swaggerProperties.getTitle(),
                swaggerProperties.getDescription(),
                swaggerProperties.getVersion(),
                swaggerProperties.getTermsOfServiceUrl(),
                contact,
                swaggerProperties.getLicense(),
                swaggerProperties.getLicenseUrl(),
                new HashSet<>()
        );
    }
}
